package com.kawa.kemanage.controller;

import com.kawa.kemanage.entity.ContentReview;
import com.kawa.kemanage.service.ContentReviewService;
import com.kawa.kemanage.util.Result;
import com.github.pagehelper.PageInfo;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.tags.Tag;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;

/**
 * 内容审核控制器
 */
@Slf4j
@RestController
@CrossOrigin
@RequestMapping({"/api/review", "/app/review"})
@Tag(name = "内容审核管理", description = "内容审核相关操作")
public class ContentReviewController {

    @Autowired
    private ContentReviewService contentReviewService;

    /**
     * 获取审核列表
     */
    @Operation(summary = "获取审核列表", description = "分页获取待审核内容列表")
    @GetMapping("/list")
    public Result getReviewList(
            @Parameter(description = "页码") @RequestParam(defaultValue = "1") Integer page,
            @Parameter(description = "每页大小") @RequestParam(defaultValue = "20") Integer size,
            @Parameter(description = "审核状态") @RequestParam(required = false) String status,
            @Parameter(description = "分类ID") @RequestParam(required = false) Integer categoryId,
            @Parameter(description = "关键词") @RequestParam(required = false) String keyword) {
        try {
            PageInfo<ContentReview> pageInfo = contentReviewService.getReviewList(status, categoryId, keyword, page, size);
            return Result.success(pageInfo);
        } catch (Exception e) {
            log.error("获取审核列表失败", e);
            return Result.error("获取审核列表失败：" + e.getMessage());
        }
    }

    /**
     * 获取审核详情
     */
    @Operation(summary = "获取审核详情", description = "根据审核ID获取审核详情")
    @GetMapping("/{reviewId}")
    public Result getReviewDetail(@Parameter(description = "审核ID") @PathVariable Long reviewId) {
        try {
            ContentReview review = contentReviewService.getReviewById(reviewId);
            return review != null ? Result.success(review) : Result.error("审核记录不存在");
        } catch (Exception e) {
            log.error("获取审核详情失败", e);
            return Result.error("获取审核详情失败：" + e.getMessage());
        }
    }

    /**
     * 审核内容
     */
    @Operation(summary = "审核内容", description = "审核通过或拒绝内容")
    @PostMapping("/{reviewId}")
    public Result reviewContent(
            @Parameter(description = "审核ID") @PathVariable Long reviewId,
            @RequestBody Map<String, Object> request) {
        try {
            //创建审核对象
            ContentReview review = new ContentReview();
            review.setReviewId(reviewId);
            review.setReviewerId(Long.valueOf(request.get("reviewerId").toString()));
            review.setReviewStatus(request.get("status").toString());
            review.setReviewComments(request.get("comments").toString());
            review.setContentId(Long.valueOf(request.get("contentId").toString()));

            boolean result = contentReviewService.reviewContent(review);
            return result ? Result.success("审核完成") : Result.error("审核失败");
        } catch (Exception e) {
            log.error("审核内容失败", e);
            return Result.error("审核内容失败：" + e.getMessage());
        }
    }

    /**
     * 批量审核
     */
    @Operation(summary = "批量审核", description = "批量审核多个内容")
    @PostMapping("/batch")
    public Result batchReview(@RequestBody Map<String, Object> request) {
        try {
            @SuppressWarnings("unchecked")
            List<Long> reviewIds = (List<Long>) request.get("reviewIds");
            String status = (String) request.get("status");
            String comments = (String) request.get("comments");
            Long reviewerId = Long.valueOf(request.get("reviewerId").toString());

            boolean result = contentReviewService.batchReview(reviewIds, status, comments, reviewerId);
            return result ? Result.success("批量审核完成") : Result.error("批量审核失败");
        } catch (Exception e) {
            log.error("批量审核失败", e);
            return Result.error("批量审核失败：" + e.getMessage());
        }
    }

    /**
     * 根据内容ID获取审核记录
     */
    @Operation(summary = "获取内容审核记录", description = "根据内容ID获取审核记录")
    @GetMapping("/content/{contentId}")
    public Result getReviewByContentId(@Parameter(description = "内容ID") @PathVariable Long contentId) {
        try {
            ContentReview review = contentReviewService.getReviewByContentId(contentId);
            return review != null ? Result.success(review) : Result.error("审核记录不存在");
        } catch (Exception e) {
            log.error("获取内容审核记录失败", e);
            return Result.error("获取内容审核记录失败：" + e.getMessage());
        }
    }
}
